
#include "div_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/platform/platform_ascendc.h"
#include <algorithm>

namespace optiling {
    const uint32_t BLOCK_SIZE = 32;
    const uint32_t BUFFER_NUM = 2;

static ge::graphStatus TilingFunc(gert::TilingContext* context)
{

  DivTilingData tiling;

    int32_t NUM = 8;
    auto ascendcPlatform = platform_ascendc::PlatformAscendC(context->GetPlatformInfo());
    uint64_t ub_size; 
    ascendcPlatform.GetCoreMemSize(platform_ascendc::CoreMemType::UB, ub_size);
    //auto aivNum = ascendcPlatform.GetCoreNum();

    uint32_t inputNum = context->GetInputShape(1)->GetStorageShape().GetShapeSize();
    uint32_t inputBytes = GetSizeByDataType(context->GetInputDesc(0)->GetDataType());
    uint32_t inputLength = inputBytes * inputNum;

    //可使用的ub空间 输入3输出1
    //uint32_t ubDataNumber = (inputBytes == 2) ? 10 : 4;

    uint32_t ubDataNumber;
    if(inputBytes == 1){
        
        ubDataNumber =10;

    }
    else if(inputBytes == 2){

        ubDataNumber =6;
    }

    else if(inputBytes == 4){

        ubDataNumber =8;
    }

    

    // The number of 32B data blocks that can be used for each data. DOUBLE BUFFER is already counted here
    uint32_t tileBlockNum = (ub_size / BLOCK_SIZE / BUFFER_NUM) / ubDataNumber;
    uint32_t tileDataNum = (tileBlockNum * BLOCK_SIZE) / inputBytes;

    // Input data for 32B alignment
    uint32_t inputLengthAlgin32 = (((inputLength + BLOCK_SIZE - 1) / BLOCK_SIZE) * BLOCK_SIZE);
    // There is at least 32B of data on each core, satisfying several settings for several cores. The maximum number of audits is the actual number of audits
    uint32_t everyCoreInputBlockNum = inputLengthAlgin32 / BLOCK_SIZE;// aivNum;
    
    //  chunks are calculated and sliced several times using the number of data on each core
    uint32_t CoreDataNum = everyCoreInputBlockNum * BLOCK_SIZE / inputBytes;
    uint32_t TileNum = everyCoreInputBlockNum / tileBlockNum;
    uint32_t finalTileNum = (everyCoreInputBlockNum % tileBlockNum) == 0 ? TileNum : TileNum + 1;
    // Tail block calculation for  chunks of data
    uint32_t TailDataNum = CoreDataNum - (tileDataNum * TileNum);
    TailDataNum = TailDataNum == 0 ? tileDataNum : TailDataNum;

    tiling.set_CoreDataNum(CoreDataNum);
    tiling.set_tileDataNum(tileDataNum);
    tiling.set_TailDataNum(TailDataNum);
    tiling.set_finalTileNum(finalTileNum);





    // uint32_t total_length = 0, min_length = context->GetInputTensor(0)->GetShapeSize();
    // for (int i = 0; i < 2; ++i) {
    //     total_length = std::max<uint32_t>(total_length, context->GetInputTensor(i)->GetShapeSize());
    //     min_length = std::min<uint32_t>(min_length, context->GetInputTensor(i)->GetShapeSize());
    // }

    // auto dt = context->GetInputTensor(0)->GetDataType();
    // uint32_t sizeofdatatype;
    // if (dt == ge::DT_INT8) {
    //     sizeofdatatype = 1;
    //     NUM = 10;
    // }
    // else if (dt == ge::DT_FLOAT16 || dt == ge::DT_FLOAT) {
    //     sizeofdatatype = 2;

    //     NUM = 6;
    // }
    // else {

    //     NUM = 8;
    //     sizeofdatatype = 4;
    // }

    // uint32_t ALIGN_NUM = BLOCK_SIZE / sizeofdatatype;
    // uint32_t tiling_size = ((ub_size) / BLOCK_SIZE / 2) / NUM;
    // tiling_size = tiling_size <= 8 ? tiling_size : tiling_size / 8 * 8;

    // uint32_t block_size = tiling_size * ALIGN_NUM;
    // if (total_length != min_length) {
    //     block_size = std::min(block_size, min_length);
    //     while (min_length % block_size || min_length % ALIGN_NUM) {
    //         block_size -= 1;
    //     }
    // }

    // aivNum = (aivNum < total_length / block_size) ? aivNum : (total_length / block_size);
    // aivNum = aivNum >= 1 ? aivNum : 1;

    // uint32_t core_size = (total_length / aivNum) / (ALIGN_NUM * 8) * (ALIGN_NUM * 8);
    // uint32_t core_remain = total_length - aivNum * core_size;

    // tiling.set_ALIGN_NUM(ALIGN_NUM);
    // tiling.set_block_size(block_size);
    // tiling.set_aivNum(aivNum);
    // tiling.set_core_size(core_size);
    // tiling.set_core_remain(core_remain);
    // tiling.set_total_length(total_length);
   // uint32_t input_data_length = context->GetInputTensor(0)->GetShapeSize();

    uint32_t x1Size = context->GetInputShape(0)->GetStorageShape().GetShapeSize();
    uint32_t x2Size = context->GetInputShape(1)->GetStorageShape().GetShapeSize();
    uint32_t ySize = context->GetOutputShape(0)->GetStorageShape().GetShapeSize();

    //获取输入shape信息
    



    if(ySize != x1Size || ySize != x2Size)
    {
        context->SetTilingKey(2);

        int32_t y_ndarray[20], x1_ndarray[20], x2_ndarray[20];
        int32_t y_dimensional, x1_dimensional, x2_dimensional;
        auto shape_y = context->GetOutputShape(0)->GetOriginShape();
        auto shape_x1 = context->GetInputTensor(0)->GetOriginShape();
        auto shape_x2 = context->GetInputTensor(1)->GetOriginShape();

        y_dimensional =  shape_y.GetDimNum();
        x1_dimensional =  shape_x1.GetDimNum();
        x2_dimensional =  shape_x2.GetDimNum();

        for(int i = 0; i < y_dimensional; i++)
        {
            y_ndarray[y_dimensional-i-1] = shape_y.GetDim(i);
            if(i<x1_dimensional) x1_ndarray[x1_dimensional-i-1] = shape_x1.GetDim(i);
            else                    x1_ndarray[i] = 1;
            if(i<x2_dimensional) x2_ndarray[x2_dimensional-i-1] = shape_x2.GetDim(i);
            else                  x2_ndarray[i] = 1;
        }
        
        tiling.set_y_dimensional(y_dimensional);
        tiling.set_y_ndarray(y_ndarray);
        tiling.set_x1_ndarray(x1_ndarray);
        tiling.set_x2_ndarray(x2_ndarray);

        int32_t y_sumndarray[20], x1_sumndarray[20], x2_sumndarray[20];
        y_sumndarray[0] = 1;
        x1_sumndarray[0] = 1;
        x2_sumndarray[0] = 1;
        for(int i = 1; i <= y_dimensional; i++)
        {
            y_sumndarray[i] = y_sumndarray[i-1]*y_ndarray[i-1];
            x1_sumndarray[i] = x1_sumndarray[i-1]*x1_ndarray[i-1];
            x2_sumndarray[i] = x2_sumndarray[i-1]*x2_ndarray[i-1];
        }
        tiling.set_y_sumndarray(y_sumndarray);
        tiling.set_x1_sumndarray(x1_sumndarray);
        tiling.set_x2_sumndarray(x2_sumndarray);
    }
    else
    {
        context->SetTilingKey(1);
    }
    
    //tiling.set_input_data_length(input_data_length);
    //tiling.set_x1_length(x1_length);
    //tiling.set_x2_length(x2_length);
















    context->SetBlockDim(1);

    tiling.SaveToBuffer(context->GetRawTilingData()->GetData(), context->GetRawTilingData()->GetCapacity());
    context->GetRawTilingData()->SetDataSize(tiling.GetDataSize());
    size_t *currentWorkspace = context->GetWorkspaceSizes(1);
    currentWorkspace[0] = 0;
    return ge::GRAPH_SUCCESS;


}
}


namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext* context)
{
    const gert::Shape* x1_shape = context->GetInputShape(0);
    gert::Shape* y_shape = context->GetOutputShape(0);
    *y_shape = *x1_shape;
    return GRAPH_SUCCESS;
}
}


namespace ops {
class Div : public OpDef {
public:
    explicit Div(const char* name) : OpDef(name)
    {
        this->Input("x1")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT8, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Input("x2")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT8, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});
        this->Output("y")
            .ParamType(REQUIRED)
            .DataType({ge::DT_FLOAT, ge::DT_FLOAT16, ge::DT_INT8, ge::DT_INT32})
            .Format({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND})
            .UnknownShapeFormat({ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND, ge::FORMAT_ND});

        this->SetInferShape(ge::InferShape);

        this->AICore()
            .SetTiling(optiling::TilingFunc);
        this->AICore().AddConfig("ascend310b");
        //this->AICore().AddConfig("ascend310p");

    }
};

OP_ADD(Div);
}
